package GUI;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * This class is responsible on creating XML file for hibernate 
 * configuration with updated user name and password
 */
public class UpdateXml 
{ 
	String userName;
	String password;
	
	
	public UpdateXml(String userName, String password) 
	{
		this.userName = userName;
		this.password = password;
	}


	/**
	 * update the XML of hibernate configuration with the userName and password of SQL 
	 */
	public void update()
	{
		String fileName = "C:\\Program Files\\m&l_dropbox\\hibernate.cfg.xml";
		File file = new File(fileName);
		try 
		{
			file.createNewFile();
			//the XML of the hibernate configuration
			String fileInfo = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> \n\t <!DOCTYPE hibernate-configuration PUBLIC    \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">\n <hibernate-configuration>\n <session-factory>\n<property name=\"connection.url\">\njdbc:mysql://localhost:3306/myDatabase\n</property>\n<property name=\"connection.driver_class\">\ncom.mysql.jdbc.Driver\n</property>\n<property name=\"connection.username\">" + userName + "</property>\n<property name=\"connection.password\">" + password + "</property>\n<!-- Set AutoCommit to true -->\n<property name=\"connection.autocommit\">\ntrue\n</property>\n<!-- SQL Dialect to use. Dialects are database specific -->\n<property name=\"dialect\">\norg.hibernate.dialect.MySQLDialect\n</property>\n<!-- Mapping files -->\n<mapping resource=\"Tag.hbm.xml\"/>\n</session-factory>\n</hibernate-configuration>";
			FileWriter f = new FileWriter(file);
			BufferedWriter b = new BufferedWriter(f);
			b.write(fileInfo);
			b.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
